import 'package:flutter/material.dart';

import 'image/cached_network_Image.dart';

class ImageSelector extends StatefulWidget {
  final List<String> productImages;
  final bool bIsNetworkImage;
  final void Function(int icount)? onClickTap;

  const ImageSelector(
      {Key? key,
      required this.productImages,
      this.bIsNetworkImage = false,
      this.onClickTap})
      : super(key: key);

  @override
  // ignore: library_private_types_in_public_api
  _ImageSelectorState createState() => _ImageSelectorState();
}

class _ImageSelectorState extends State<ImageSelector> {
  int _selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    return widget.productImages.isNotEmpty
        ? SizedBox(
            height: 80,
            child: ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: widget.productImages.length,
              itemBuilder: (BuildContext context, int index) {
                return GestureDetector(
                  onTap: () {
                    // 处理图片点击事件
                    setState(() {
                      _selectedIndex = index;
                    });

                    if (widget.onClickTap != null) {
                      widget.onClickTap!(index);
                    }
                  },
                  child: Container(
                    margin: const EdgeInsets.all(8.0),
                    padding: const EdgeInsets.all(4),
                    width: 90, // 每张图片的宽度
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(4.0),
                      border: _selectedIndex == index
                          ? Border.all(
                              color: Colors.black, // 选中时的边框颜色
                              width: 1.5, // 选中时的边框宽度
                            )
                          : Border.all(
                              color: Colors.grey, // 选中时的边框颜色
                              width: 0.5, // 选中时的边框宽度
                            ),
                      image: !widget.bIsNetworkImage
                          ? DecorationImage(
                              //image: NetworkImage(widget.productImages[index]),
                              image: AssetImage(widget.productImages[index]),
                              fit: BoxFit.cover,
                            )
                          : null,
                    ),
                    child: widget.bIsNetworkImage
                        ? CachedNetworkImageLoader(
                            imageUrl: widget.productImages[index],
                          )
                        : Container(),
                  ),
                );
              },
            ),
          )
        : Container();
  }
}
